import 'package:flutter/material.dart';
import 'package:flutter_rider/main/widgets/base/search_widget.dart';
import 'package:flutter_rider/main/widgets/ext/u_layout.dart';
import 'package:flutter_rider/main/widgets/ext/u_text.dart';
import 'package:flutter_rider/main/widgets/ext/u_title_layout.dart';
import 'package:flutter_rider/main/widgets/state/refreshable_layout.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/module/mine/mine_router.dart';
import 'package:flutter_rider/routers/fluro_navigator.dart';
import 'package:flutter_rider/utils/screen_fit.dart';

class ShuntimeLogPage extends StatefulWidget {
  const ShuntimeLogPage({Key? key}) : super(key: key);

  @override
  State<ShuntimeLogPage> createState() => _ShuntimeLogPageState();
}

class _ShuntimeLogPageState extends State<ShuntimeLogPage> {
  final _stateController = RefreshableLayoutController();
  final _inputController = TextEditingController(text: "");
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: UIColor.color_main,
        appBar: UTitleLayout.getBackTitle(context, "调车日志"),
        body: SafeArea(
            child: Column(
          children: [
            SearchWidget(
              "搜索车辆编号、操作人",
              textEditingController: _inputController,
              onEditingComplete: () {
                _stateController.reload();
              },
            ),
            // _renderListItem("test", 1),
            _renderList(),
          ],
        )));
  }

  Widget _renderList() {
    return Expanded(
        child: RefreshableLayout(
            controller: _stateController,
            urlCallback: _buildUrl,
            itemBuilder: _renderListItem));
  }

  String _buildUrl() {
    var baseUrl = "/user/car/dispatch_list";
    var searchKey = _inputController.text.trim();
    if (searchKey.isNotEmpty) {
      if (!baseUrl.contains("?")) {
        baseUrl += "?";
      } else {
        baseUrl += "&";
      }
      baseUrl += "key=${Uri.encodeComponent(searchKey)}";
    }
    return baseUrl;
  }

  Widget _renderListItem(dynamic data, int index) {
    return GestureDetector(
      onTap: () {
        NavigatorUtils.push(context, MineRouter.shuntimeDetailPage,
            arguments: data["id"]);
      },
      child: Container(
        decoration: BoxDecoration(
          color: UIColor.color_content,
          borderRadius: BorderRadius.circular(12.rpx),
        ),
        margin: EdgeInsets.only(top: 10.rpx, left: 12.rpx, right: 12.rpx),
        child: Row(
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                UText.get(
                  data["car_no"],
                  UIColor.color_t16,
                  16.rpx,
                ).padding(EdgeInsets.only(left: 12.rpx, top: 12.rpx)),
                4.hspace,
                UText.get(data["created_time"], UIColor.color_t17, 12.rpx)
                    .padding(EdgeInsets.only(left: 12.rpx, bottom: 12.rpx)),
              ],
            ),
            // const Spacer(),
            Container(
              alignment: Alignment.centerRight,
              child: Text(
                data["operation_name"],
                maxLines: 1,
                style: TextStyle(
                    color: UIColor.color_t3,
                    fontSize: 14.rpx,
                    overflow: TextOverflow.ellipsis),
              ),
            ).expand,
            // UText.get(data["operation_name"], UIColor.color_t3, 14.rpx).expand,
            Icon(Icons.navigate_next, size: 20.rpx, color: UIColor.color_t3),
            12.wspace
          ],
        ),
      ),
    );
  }
}
